<template>
	<view>
		<!-- 自定义导航栏 -->
		<z-nav-bar transparentFixedFontColor="#000" type="transparentFixed" title="购买红心"></z-nav-bar>
		<!-- 公共组件-每个页面必须引入 -->
		<public-module></public-module>
		<view class="h-bg" :style="'background-image: url('+headBgImg+');'">
			
		</view>
		<view class="main">
			<view class="balance flex align-item j-between">
				<view class="balance-left flex align-item">
					<view class="balance-icon">
						<image :src="imgUrl + 'static/web/icon/612c0a7e34184f8bd41931f2aa963cc.png'" mode=""></image>
					</view>
					<view class="balance-tit">
						可用余额：
					</view>
					<view class="balance-num">
						{{ data.virtual_currency_balance || 0 }} 红心
					</view>
				</view>
				<view class="balance-right flex align-item" @click="goRecord">
					<view class="balance-right-tit">
						充值记录
					</view>
					<view class="balance-right-icon">
						<image :src=" imgUrl + 'static/web/icon/me_lise_more.png'" mode=""></image>
					</view>
				</view>
			</view>
			<view class="flex align-item item-list">
				<view class="item flex align-item column" :class="item.id == selectId ? 'tuijian' : ''" v-for="(item,index) in data.list" :key="index" @click="selectLB(item.id)">
					<view class="day">
						<text class="tit">{{ item.name }}</text>红心
					</view>
					<view class="day-price">
						¥{{ item.price }}
					</view>
				</view>
			</view>
			<view class="inpu">
				<input class="input" type="text" @input="onKeyInput" v-model="num" placeholder="自定义红心数量" placeholder-style="font-size:28rpx;color:#BEC6D3;font-weight: 400;font-family: PingFangSC-Regular, PingFang SC;" maxlength="5" />
			</view>
			<view class="tishi">
				{{ data.proportion || 0 }}红心 = 1.00元人民币
			</view>
		</view>
		<view class="xieyi flex align-item">
			<checkbox value="" :checked="checked" style="transform:scale(0.7);"  class="round red" @click="checkChange"/>
			<view>
				我已阅读并同意 <text class="xieyi-tit" @click="goXieyi">《红心用户协议》</text>
			</view>
		</view>
		<view class="invitation flex">
			<view class="invitation-btn invitation-com" v-if="num == '' && selectId == ''">
				立即充值
			</view>
			<view class="invitation-btn-on invitation-com" v-else @click="$u.throttle(purchaseLiB,1000)">
				立即充值
			</view>
		</view>
	</view>
</template>

<script>
	import { mapState, mapMutations } from 'vuex';
	// #ifdef H5
	// import { wxPublicPay } from '@/config/html5Utils.js'
	// #endif
	import { setZhPay } from '@/config/utils.js'
export default {
	data() {
		return {
			imgUrl: this.$http.baseUrl, // 服务器域名
			headBgImg: this.$http.baseUrl + 'static/web/img/apply-bg.png', // 顶部背景
			uid: '', //用户id
			checked: false, // 选中框默认false
			num: '', // 自定义的红心数量
			selectId: '', // 选中的红心id
			data: [], // 存储获取的信息
		};
	},
	computed: {
		...mapState(['userInfo']),
	},
	onLoad(e) {
		var userInfo = this.$store.state.userInfo;
		this.uid = userInfo.uid;
		this.getVirtualCurrency()
	},
	//页面显示
	onShow() {
		
	},
	onReady() {
		
	},
	// 触底触发
	onReachBottom() {
		
	},
	// 下拉刷新
	onPullDownRefresh(){
		
	},
	//方法
	methods: {
		// 选中
		checkChange(){
			this.checked = !this.checked
		},
		// 获取红心充值套餐
		getVirtualCurrency(){
			var that = this
			if(!that.uid){
				uni.showToast({
				    title: '请前往授权登录',
					icon: 'none',
				    duration: 1000
				});
				setTimeout(function() {
					uni.switchTab({
						url: '/pages/my/my'
					});
				}, 1000);
			}else{
				that.$http.get('api/ZhVirtualCurrency/index').then(res=> {
					if(res){
						that.data = res
					}
				})
			}
		},
		// 选中红心菜单
		selectLB(i){
			this.num = ''
			this.selectId = i
		},
		// 自定义时将选中的取消
		onKeyInput(e){
			this.selectId = ''
			e.target.value = e.target.value.replace(/^0|[^\d]|[.]/g, '')
			this.$nextTick(() => {
				this.num = e.target.value
			})
		},
		// 查看用户协议
		goXieyi(){
			uni.navigateTo({
				url: '/pages/my/user_agreement/user_agreement?type=' + 2
			})
		},
		// 购买红心
		purchaseLiB(){
			console.log('点击了')
			var that = this
			if(!that.selectId && !that.num){
				uni.showToast({
					title: '请选择或输入需要购买的红心数量',
					icon: 'none'
				})
				return false
			}
			if(!that.checked){
				uni.showToast({
					title: '请先阅读并勾选红心用户协议',
					icon: 'none'
				})
				return false
			}
			if(that.selectId && that.num){
				uni.showToast({
					title: '请求支付失败',
					icon: 'none'
				})
				return false
			}
			var type = ''
			// #ifdef MP-WEIXIN
			type = 'wx'
			// #endif
			// #ifdef APP-PLUS
			type = 'app'
			// #endif
			// #ifdef H5
			type = 'H5'
			if(this.isWxBrowser){
				type = 'H5-wx'
			}
			// #endif
			that.$http.post('api/ZhGiftOrder/add',{
				'id':that.selectId,
				'num':that.num,
				'type': type
			}).then(res => {
				console.log(res,'0000')
				if(res){
					setZhPay(res.paydata,()=>{
						that.getVirtualCurrency()
						that.selectId = ''
						that.num = ''
						uni.showToast({
							title:'充值红心成功',
							icon: 'none'
						})
					})
					// // #ifdef MP-WEIXIN
					// uni.requestPayment({
					// 	provider: 'wxpay',
					// 	timeStamp: res.paydata.timestamp,
					// 	nonceStr: res.paydata.nonceStr,
					// 	package: res.paydata.package,
					// 	signType: res.paydata.signType,
					// 	paySign: res.paydata.paySign,
					// 	success: function (res) {
					// 		that.getVirtualCurrency()
					// 		that.selectId = ''
					// 		that.num = ''
					// 		uni.showToast({
					// 			title:'充值红心成功',
					// 			icon: 'none'
					// 		})
					// 	},
					// 	fail: function (err) {
					// 		console.log('fail:' + JSON.stringify(err));
					// 	}
					// });
					// // #endif
					
					// // #ifdef H5
					// if(this.isWxBrowser){
					// 	wxPublicPay(res.paydata,()=>{
					// 		that.getVirtualCurrency()
					// 		that.selectId = ''
					// 		that.num = ''
					// 		uni.showToast({
					// 			title:'充值红心成功',
					// 			icon: 'none'
					// 		})
					// 	})
					// }else{
					// 	location.href = res.paydata + '&redirect_url=' + decodeURIComponent(location.href)
					// }
					// // #endif
				}
			});
		},
		// 前往充值记录
		goRecord(){
			uni.navigateTo({
				url: '/pages/my/record/record?type=1'
			})
		}
	},
	//页面隐藏
	onHide() {},
	//页面卸载
	onUnload() {},
	//用户点击分享
	onShareAppMessage(e) {
		return this.wxShare();
	}
};
</script>
<style>
page {
	background-color: #FFFFFF;
}
</style>
<style lang="scss" scoped>
.flex {
	display: flex;
}
.align-item {
	align-items: center;
}
.j-between {
	justify-content: space-between;
}
.column {
	flex-direction: column;
}

.h-bg {
	width: 100%;
	padding: 190rpx 0 0;
	// #ifdef H5
	padding: 120rpx 0 0;
	// #endif
}
.main {
	padding: 0 30rpx;
	.balance {
		margin-top: 30rpx;
		.balance-left {
			.balance-icon {
				width: 40rpx;
				height: 40rpx;
				margin-right: 10rpx;
				image {
					width: 100%;
					height: 100%;
				}
			}
			.balance-tit {
				font-size: 28rpx;
				
			}
			.balance-num {
				color: #7F5AFF;
				font-size: 32rpx;
				font-size: 500;
			}
		}
		.balance-right {
			.balance-right-tit {
				font-size: 28rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #000013;
			}
			.balance-right-icon {
				width: 11rpx;
				height: 20rpx;
				margin-left: 16rpx;
				image {
					width: 100%;
					height: 100%;
				}
			}
		}
	}
	.inpu {
		margin-top: 60rpx;
		.input {
			border-bottom: 2rpx solid #7F5AFF;
			padding-bottom: 10rpx;
		}
	}
	.tishi {
		font-size: 24rpx;
		font-weight: 400;
		color: #BEC6D3;
		font-family: PingFangSC-Regular, PingFang SC;
		margin-top: 60rpx;
	}
	.item-list {
		flex-wrap: wrap;
		.item {
			width: 30%;
			padding: 40rpx 0;
			background-color: #FFFFFF;
			border-radius: 16rpx;
			margin: 30rpx 30rpx 0 0;
			border: 2rpx solid #CCCCCC;
			box-sizing: border-box;
			.day {
				width: 100%;
				text-align: center;
				font-size: 26rpx;
				font-weight: 500;
				color: #000013;
				.tit {
					font-size: 40rpx;
					margin-right: 6rpx;
				}
			}
			.day-price {
				width: 100%;
				text-align: center;
				font-size: 26rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #6A6A7E;
				margin-top: 10rpx;
			}
		}
		.item:nth-child(3n){
			margin-right: 0;
		}
		.tuijian {
			position: relative;
			border: 2rpx solid #7F5AFF;
			background-color: #7F5AFF;
			
			.day {
				color: #FFFFFF;
			}
			.day-price {
				color: #F9F9F9;
			}
		}
	}
}
.xieyi {
	justify-content: center;
	margin-top: 450rpx;
	// #ifdef H5
	margin-top: 200rpx;
	// #endif
	font-size: 28rpx;
	color: #6A6A7E;
	.xieyi-tit {
		color: #7F5AFF;
	}
}
.invitation {
	justify-content: center;
	margin-top: 40rpx;
	background-color: #FFFFFF;
	padding: 0 0 20rpx;
	.invitation-com {
		width: 82%;
		height: 80rpx;
		text-align: center;
		line-height: 80rpx;
		border-radius: 16rpx;
		font-size: 28rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #FFFFFF;
	}
	.invitation-btn {
		background-color: #BEC6D3;
	}
	.invitation-btn-on {
		background: #7F5AFF;
	}
}

</style>
